using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Configuration;

namespace ProcessesSMS
{
	/// <summary>
	/// Summary description for.
	/// Project name: SMS_xoso
	/// channel: n/a
	/// command code: HN X Y
	/// created: 01/09/09 .
	/// Writer : Nampt + Tuanbc
	/// Company: VIT TELECOM
	/// </summary>

	public class XOSO_CAU
	{
		#region Declare
		private SMS_MO WMO;
		private SMS_MT WMT;
		ArrayList arr = new ArrayList();
		public XOSO_CAU(SMS_MO MO)
		{
			WMO = MO;
		}

		public XOSO_CAU()
		{
		}
		#endregion
		public void run()
		{
			WMT = new SMS_MT(WMO);
			string tmpMsg = WMO.Message.Substring(WMO.CommandCode.Length).Trim().Replace("  ", " ").Replace("  ", " ");
			tmpMsg = tmpMsg.Replace("  ", " ").Trim().ToUpper();
			string[] msg = tmpMsg.Split(new char[] { ' ' });

			string strReturnMsg = "";            
			strReturnMsg = GetReturnMsg(msg);
			if(strReturnMsg!="")
			{
				WMT.AddMessage(WMO.UserID, strReturnMsg, SMS_ContentType.SMS_TEXT);
				WMT.MsgType = SMS_MsgType.SMS_TINHTIEN;
				WMT.Insert(); 
			}
			return;          
		}
		public string GetReturnMsg(string[] arr)
		{
			string connStr = ConfigurationSettings.AppSettings["CONNECTION_STRING_7x62"].ToString();
			SqlConnection conn = new SqlConnection(connStr);
			string strSQL;
			string strSQLDD;
			int flag;
            string strnumber = "";
            string strchar = "";
			string msgBody="";
			string d0="", d1="", d2="", d3="", d4="", d5="", d6="", d7="", d8="", d9="";
			string _d0="", _d1="", _d2="", _d3="", _d4="", _d5="", _d6="", _d7="", _d8="", _d9="";
            arr[0] = arr[0].ToUpper();
			if (arr[0] =="MB" || arr[0]=="TD" || arr[0]=="HN" || arr[0]=="QN" || arr[0]=="BN" || arr[0]=="HP" || arr[0]=="ND" || arr[0]=="TB" || arr[0]=="")
			{
				strSQL = "select top 1 * from xoso where mamien='mb' order by ngay desc";
				strSQLDD = "SELECT Top 1 GuessBridge FROM XOSO_DD WHERE Mamien='MB'";
				flag=0;
			}
			else if (arr[0]=="BDH" || arr[0]=="DNG" || arr[0]=="DLK" || arr[0]=="DNO" || arr[0]=="KH" || arr[0]=="KT" || arr[0]=="PY" || arr[0]=="NT" || arr[0]=="NA" || arr[0]=="GL" || arr[0]=="QB" || arr[0]=="QNM" || arr[0]=="QNI" || arr[0]=="BDH" || arr[0]=="QT" || arr[0]=="TTH")
			{
				strSQL = "select top 1 * from xoso where matinh='"+ arr[0] +"' and mamien='mt' order by ngay desc";
				strSQLDD = "SELECT * FROM XOSO_DD WHERE Mamien='MT' AND Matinh='" + arr[0] +"' order by modify";
				flag=1;
			}
			else
			{
				strSQL = "select top 1 * from xoso where matinh='" + arr[0] +"' and mamien='mn' order by ngay desc";
				strSQLDD = "SELECT * FROM XOSO_DD WHERE Mamien='MN' AND Matinh='" + arr[0] +"' order by modify";
				flag=2;
			}
			#region Access Data
			conn.Open();
			SqlDataAdapter da = new SqlDataAdapter(strSQL,conn);
			DataSet ds = new DataSet();
			da.Fill(ds,"XOSO");			
			conn.Close();
            if (ds.Tables[0].Rows.Count<=0)
            {
                msgBody="De nhan CAU SO TUNG BUNG cung 7x62 soan tin: CAU Matinh gui 7562.VD:De nhan CAU SO TUNG BUNG cua mien Bac soan tin: CAU TD gui 7562";
                return msgBody;
            }
            string GiaiDB = ds.Tables[0].Rows[0]["GiaiDacBiet"].ToString().Trim();
            string Giai1 = ds.Tables[0].Rows[0]["GiaiNhat"].ToString().Trim();
            string Giai2 = ds.Tables[0].Rows[0]["GiaiNhi"].ToString().Trim();
            string Giai3 = ds.Tables[0].Rows[0]["GiaiBa"].ToString().Trim();
            string Giai4 = ds.Tables[0].Rows[0]["GiaiTu"].ToString().Trim();
            string Giai5 = ds.Tables[0].Rows[0]["GiaiNam"].ToString().Trim();
            string Giai6 = ds.Tables[0].Rows[0]["GiaiSau"].ToString().Trim();
            string Giai7 = ds.Tables[0].Rows[0]["GiaiBay"].ToString().Trim();
            string Giai8 = ds.Tables[0].Rows[0]["GiaiTam"].ToString().Trim();
			#endregion
            if ((WMO.UserID == "84978925568") || (WMO.UserID == "84902222776") || (WMO.ServiceID.Replace("04", "") == "7562") || (WMO.ServiceID.Replace("04", "") == "7662") || (WMO.ServiceID.Replace("04", "") == "7762"))
			{
				if(flag ==0)
				{
					#region MIEN BAC
					string gdb = GiaiDB.Substring(3); //56
					string g1 = Giai1.Substring(3); //82
					string[] g2 = Giai2.Split('-'); //0: 00 ___ 1: 11
					string[] g3 = Giai3.Split('-'); //0: 09 ___ 1: 73 ___ 2: 36 ___ 3: 97 __ 4: 33 ___ 5: 89
					string[] g4 = Giai4.Split('-'); //0: 86 ___ 1: 63 ___ 2: 40 ___ 3: 75
					string[] g5 = Giai5.Split('-'); //0: 61 ___ 1: 82 ___ 2: 25 ___ 3: 76
					string[] g6 = Giai6.Split('-'); //0: 09 ___ 1: 31 ___ 2: 02
					string[] g7 = Giai7.Split('-'); //0: 44 ___ 1: 96 ___ 2: 17 ___ 3: 65
					string[] ALL = new string[] {gdb,
                        g1,
                        g2[0].Substring(3), g2[1].Substring(3),
                        g3[0].Substring(3), g3[1].Substring(3), g3[2].Substring(3), g3[3].Substring(3), g3[4].Substring(3), g3[5].Substring(3),
                        g4[0].Substring(2), g4[1].Substring(2), g4[2].Substring(2), g4[3].Substring(2),
                        g5[0].Substring(2), g5[1].Substring(2), g5[2].Substring(2), g5[3].Substring(2),
                        g6[0].Substring(1), g6[1].Substring(1), g6[2].Substring(1),
                        g7[0], g7[1], g7[2], g7[3]};
                    for (int i = 0; i < ALL.Length; i++)
                    {
                        strnumber = ALL[i];
                        strchar = ALL[i].Substring(1);
                        if (int.Parse(strnumber) < 10)
                        {
                            //_d0 += ALL[i].Substring(1).Replace(" ","") + " ";
                            d0 = d0 + strchar + " ";
                        }
                        else if ((int.Parse(strnumber) >= 10) && (int.Parse(strnumber) < 20))
                        {
                            //_d1 += ALL[i].Substring(1).Replace(" ","") + " ";
                            d1 = d1 + strchar + " ";
                        }
                        else if ((int.Parse(strnumber) >= 20) && (int.Parse(strnumber) < 30))
                        {
                            //_d2 += ALL[i].Substring(1).Replace(" ", "") + " ";
                            d2 = d2 + strchar + " ";
                        }
                        else if ((int.Parse(strnumber) >= 30) && (int.Parse(strnumber) < 40))
                        {
                            //_d3 += ALL[i].Substring(1).Replace(" ", "") + " ";
                            d3 = d3 + strchar + " ";
                        }
                        else if ((int.Parse(strnumber) >= 40) && (int.Parse(strnumber) < 50))
                        {
                            //_d4 += ALL[i].Substring(1).Replace(" ", "") + " ";
                            d4 = d4 + strchar + " ";
                        }
                        else if ((int.Parse(strnumber) >= 50) && (int.Parse(strnumber) < 60))
                        {
                           // _d5 += ALL[i].Substring(1).Replace(" ", "") + " ";
                            d5 = d5 + strchar + " ";
                        }
                        else if ((int.Parse(strnumber) >= 60) && (int.Parse(strnumber) < 70))
                        {
                            //_d6 += ALL[i].Substring(1).Replace(" ", "") + " ";
                            d6 = d6 + strchar + " ";
                        }
                        else if ((int.Parse(strnumber) >= 70) && (int.Parse(strnumber) < 80))
                        {
                            //_d7 += ALL[i].Substring(1).Replace(" ", "") + " ";
                            d7 = d7 + strchar + " ";
                        }
                        else if ((int.Parse(strnumber) >= 80) && (int.Parse(strnumber) < 90))
                        {
                            //_d8 += ALL[i].Substring(1).Replace(" ", "") + " ";
                            d8 = d8 + strchar + " ";
                        }
                        else
                        {
                            //_d9 += ALL[i].Substring(1).Replace(" ", "") + " ";
                            d9 = d9 + strchar + " ";
                        }
                    }
                    //d0 = d0.Trim().Replace(" ",",");
                    msgBody = "Thong ke theo dau\n0: " + d0 + "\n1: " + d1 + "\n2: " + d2 + "\n3: " + d3 + "\n4: " + d4 + "\n5: " + d5 + "\n6: " + d6 + "\n7: " + d7 + "\n8: " + d8 + "\n9: " + d9;
					#endregion
				}
				else if (flag==1)
				{
					#region MIEN TRUNG
					string gidb = GiaiDB.Substring(3);
					string gi1 = Giai1.Substring(3);
					string gi2 = Giai2.Substring(3);
					string[]  gi3 = Giai3.Split('-');
					string[] gi4 = Giai4.Split('-');
					string gi5 = Giai5.Substring(2);
					string[]  gi6 = Giai6.Split('-');
					string gi7 = Giai7.Substring(1);
					string[] ALLMT = new string[] {gidb, gi1, gi2, gi3[0].Substring(3), gi3[1].Substring(3),gi4[0].Substring(3),gi4[1].Substring(3),gi4[2].Substring(3),gi4[3].Substring(3),gi4[4].Substring(3),gi4[5].Substring(3),gi4[6].Substring(3),gi5, gi6[0].Substring(2), gi6[1].Substring(2),gi6[2].Substring(2),gi7,Giai8 };
                    for (int i = 0; i < ALLMT.Length; i++)
					{
                        strnumber = ALLMT[i];
                        strchar = ALLMT[i].Substring(1);
                        if (int.Parse(strnumber) < 10)
                        {
                            //_d0 += ALL[i].Substring(1).Replace(" ","") + " ";
                            d0 = d0 + strchar + " ";
                        }
                        else if ((int.Parse(strnumber) >= 10) && (int.Parse(strnumber) < 20))
                        {
                            //_d1 += ALL[i].Substring(1).Replace(" ","") + " ";
                            d1 = d1 + strchar + " ";
                        }
                        else if ((int.Parse(strnumber) >= 20) && (int.Parse(strnumber) < 30))
                        {
                            //_d2 += ALL[i].Substring(1).Replace(" ", "") + " ";
                            d2 = d2 + strchar + " ";
                        }
                        else if ((int.Parse(strnumber) >= 30) && (int.Parse(strnumber) < 40))
                        {
                            //_d3 += ALL[i].Substring(1).Replace(" ", "") + " ";
                            d3 = d3 + strchar + " ";
                        }
                        else if ((int.Parse(strnumber) >= 40) && (int.Parse(strnumber) < 50))
                        {
                            //_d4 += ALL[i].Substring(1).Replace(" ", "") + " ";
                            d4 = d4 + strchar + " ";
                        }
                        else if ((int.Parse(strnumber) >= 50) && (int.Parse(strnumber) < 60))
                        {
                            // _d5 += ALL[i].Substring(1).Replace(" ", "") + " ";
                            d5 = d5 + strchar + " ";
                        }
                        else if ((int.Parse(strnumber) >= 60) && (int.Parse(strnumber) < 70))
                        {
                            //_d6 += ALL[i].Substring(1).Replace(" ", "") + " ";
                            d6 = d6 + strchar + " ";
                        }
                        else if ((int.Parse(strnumber) >= 70) && (int.Parse(strnumber) < 80))
                        {
                            //_d7 += ALL[i].Substring(1).Replace(" ", "") + " ";
                            d7 = d7 + strchar + " ";
                        }
                        else if ((int.Parse(strnumber) >= 80) && (int.Parse(strnumber) < 90))
                        {
                            //_d8 += ALL[i].Substring(1).Replace(" ", "") + " ";
                            d8 = d8 + strchar + " ";
                        }
                        else
                        {
                            //_d9 += ALL[i].Substring(1).Replace(" ", "") + " ";
                            d9 = d9 + strchar + " ";
                        }
                    }
                    //d0 = d0.Trim().Replace(" ",",");
                    msgBody = "Thong ke theo dau\n0: " + d0 + "\n1: " + d1 + "\n2: " + d2 + "\n3: " + d3 + "\n4: " + d4 + "\n5: " + d5 + "\n6: " + d6 + "\n7: " + d7 + "\n8: " + d8 + "\n9: " + d9;
                    #endregion
				}
				else
				{
					#region MIEN NAM
					string gidb = GiaiDB.Substring(4);
					string gi1 = Giai1.Substring(3);
					string gi2 = Giai2.Substring(3);
					string[]  gi3 = Giai3.Split('-');
					string[] gi4 = Giai4.Split('-');
					string gi5 = Giai5.Substring(2);
					string[]  gi6 = Giai6.Split('-');
					string gi7 = Giai7.Substring(1);
					string[] ALLMN = new string[] {gidb, gi1, gi2, gi3[0].Substring(3), gi3[1].Substring(3),gi4[0].Substring(3),gi4[1].Substring(3),gi4[2].Substring(3),gi4[3].Substring(3),gi4[4].Substring(3),gi4[5].Substring(3),gi4[6].Substring(3),gi5, gi6[0].Substring(2), gi6[1].Substring(2),gi6[2].Substring(2),gi7,Giai8 };
                    for (int i = 0; i < ALLMN.Length; i++)
					{
                        strnumber = ALLMN[i];
                        strchar = ALLMN[i].Substring(1);
                        if (int.Parse(strnumber) < 10)
                        {
                            //_d0 += ALL[i].Substring(1).Replace(" ","") + " ";
                            d0 = d0 + strchar + " ";
                        }
                        else if ((int.Parse(strnumber) >= 10) && (int.Parse(strnumber) < 20))
                        {
                            //_d1 += ALL[i].Substring(1).Replace(" ","") + " ";
                            d1 = d1 + strchar + " ";
                        }
                        else if ((int.Parse(strnumber) >= 20) && (int.Parse(strnumber) < 30))
                        {
                            //_d2 += ALL[i].Substring(1).Replace(" ", "") + " ";
                            d2 = d2 + strchar + " ";
                        }
                        else if ((int.Parse(strnumber) >= 30) && (int.Parse(strnumber) < 40))
                        {
                            //_d3 += ALL[i].Substring(1).Replace(" ", "") + " ";
                            d3 = d3 + strchar + " ";
                        }
                        else if ((int.Parse(strnumber) >= 40) && (int.Parse(strnumber) < 50))
                        {
                            //_d4 += ALL[i].Substring(1).Replace(" ", "") + " ";
                            d4 = d4 + strchar + " ";
                        }
                        else if ((int.Parse(strnumber) >= 50) && (int.Parse(strnumber) < 60))
                        {
                            // _d5 += ALL[i].Substring(1).Replace(" ", "") + " ";
                            d5 = d5 + strchar + " ";
                        }
                        else if ((int.Parse(strnumber) >= 60) && (int.Parse(strnumber) < 70))
                        {
                            //_d6 += ALL[i].Substring(1).Replace(" ", "") + " ";
                            d6 = d6 + strchar + " ";
                        }
                        else if ((int.Parse(strnumber) >= 70) && (int.Parse(strnumber) < 80))
                        {
                            //_d7 += ALL[i].Substring(1).Replace(" ", "") + " ";
                            d7 = d7 + strchar + " ";
                        }
                        else if ((int.Parse(strnumber) >= 80) && (int.Parse(strnumber) < 90))
                        {
                            //_d8 += ALL[i].Substring(1).Replace(" ", "") + " ";
                            d8 = d8 + strchar + " ";
                        }
                        else
                        {
                            //_d9 += ALL[i].Substring(1).Replace(" ", "") + " ";
                            d9 = d9 + strchar + " ";
                        }
                    }
                    //d0 = d0.Trim().Replace(" ",",");
                    msgBody = "Thong ke theo dau\n0: " + d0 + "\n1: " + d1 + "\n2: " + d2 + "\n3: " + d3 + "\n4: " + d4 + "\n5: " + d5 + "\n6: " + d6 + "\n7: " + d7 + "\n8: " + d8 + "\n9: " + d9;
                    #endregion
				}
				#region Dự  đoán các cặp “số đẹp”,các bộ xiên sẽ xuất hiện trong lần mở thưởng tiếp theo.
				conn.Open();
				SqlDataAdapter oda = new SqlDataAdapter(strSQLDD,conn);
				DataSet ods = new DataSet();
				oda.Fill(ods);
				DataTable odt = ods.Tables[0];
				conn.Close();
				string guessbridge = odt.Rows[0]["GuessBridge"].ToString();
				msgBody = msgBody + "\nCAU SO TUNG BUNG\n" + guessbridge;
				#endregion
			}
			else
			{
                msgBody = "De nhan CAU SO TUNG BUNG cung 7x62 soan tin:CAU Matinh gui 7562.VD:De nhan CAU SO TUNG BUNG cua mien Bac soan tin:CAU TD gui 7562";
			}
			return msgBody;
		}
	}		
}
